Introducing JavaScript

Welcome to the exciting world of JavaScript Programming. Before you get started learning about JavaScript's bells and whistles, you probably have a number of questions. This section will address common JavaScript questions.

What is JavaScript?

JavaScript is a compact language developed by Netscape that puts new scripting power directly into the hands of HTML authors. JavaScript statements are embedded into HTML documents--the Netscape Navigator interprets your scripts in the same way that it understands HTML tags. Authors can extend the capacity of buttons, anchors, text input, etc. to flexibly recognize and respond to user commands.

Among many other features, JavaScript provides for

What is JScript?

JScript is Microsoft's version of the original scripting language developed by Netscape. It supports all of the features of standard JavaScript, and has greater access to the document model supported by Internet Explorer, the browser in which the scripting engine resides.

What is ECMAScript?

ECMAScript is the name of the language described in the standard ECMA-262, created by the European Computer Manufacturer's Association. This standard specifies the expected behavior of a JavaScript/JScript engine, as the interpreter for a programming language. It does not specify the document model provided by the browser, what page elements are scriptable, or in what form user-interaction events should be propagated to a script. (These latter bits of information are set forth in the W3C Document Object Model (DOM) specification.) Keep in mind that JavaScript is constantly evolving; the language is growing and changing even as you use this CD-ROM. Early versions of Netscape Navigator 2.0 and 3.0 were unstable and crashed frequently. Since that time, the browser engines have become more powerful, faster, and more robust.

You must also remember that Netscape and Microsoft do not support identical versions of the scripting language, nor equally-capable versions of the JavaScript object model. This can influence design and coding decisions when you sit down to write a script.



Which browsers support JavaScript?

The following browsers support JavaScript (or the Microsoft version, called JScript):



How do I include JavaScript in HTML Documents?

The SCRIPT tag lets you place JavaScript directives into your HTML documents. This script adds the words "Hello world!" into a new document.
<H3>Greet the World</H3>

<!-- Here's some Java Script -->

<SCRIPT LANGUAGE="JavaScript">
document.write("Hello world!")
</SCRIPT>
This is how it looks when Netscape Navigator or Internet Explorer loads this HTML and JavaScript

Greet the World



How do I hide JavaScript from old browsers?

It's easy to hide JavaScript by "commenting" it out. Just surround your JavaScript code as follows. The first comment has no "close," -->. Since JavaScript ignores everything on a line that starts with either <!-- or //, and other browsers ignore all text that fall between <!-- and -->, this scheme effectively "hides" JavaScript code from old browsers. Most browsers ignore the second <!--. After finding a comment-open they ignore everything except a comment-close.
<SCRIPT LANGUAGE="JavaScript">
<!-- hide from old browsers

function hello_world()
{
document.write("<b>Hello World!</b><p>")
}


<!-- done hiding --></SCRIPT>

Do I need to rename my files?

No, you don't. JavaScript directives can be included in any normal HTML document.



Do I need a special account to run JavaScript?

No. Unlike Common Gateway Interface (CGI) programming, JavaScript allows you to write functioning applets without any need for "special" accounts. JavaScript is placed within normal HTML documents.



What is the difference between JavaScript and CGI programs?

CGI programs run on Web Servers. In contrast, your JavaScript programs run on your client's machine. When your reader's browser downloads your HTML files, it also downloads the JavaScript code and executes it (interprets it) on that machine. This makes system compatability crucial. Although you develop your HTML and JavaScript on, say, Windows 95, your readers may visit your pages from Unix, Macintosh, WinNT, or OS/2 machines.



Can everyone see my JavaScript code?

Generally, yes. There is no "privacy" with JavaScript. Unlike CGI and Java programming, JavaScript code is sent in source code form to your client's machine. You can sometimes "hide" JavaScript code in "invisible" HTML frames, or include a .js file in a <SCRIPT> tag SRC attribute (<SCRIPT SRC="myJScript.js">), but these methods don't always work.

Microsoft's Internet Explorer 5 now supports script-encryption (through a special add-on module available at the Microsoft JScript Website).



What is the difference between JavaScript and Java?

JavaScript resembles Sun Microsystem's Java, but they are really quite different. Java is a full-featured, object-oriented language, which creates stand-alone applications. JavaScript is a small, object-based scripting language, which extends HTML. JavaScript is simpler than Java, and much, much smaller. Java is similar to C++ and is meant to be used by professional programmers. JavaScript, in contrast, was designed for easy use and is meant for the rest of us.

Java JavaScript
Java applets must be compiled on a server machine before a client can download and run them. JavaScript is interpreted (in the newer browsers, the script is compiled "on-the-fly"); browsers can understand JavaScript directives in HTML documents.
Java is object-oriented. Java supports classes and inheritance JavaScript is object-based. You can assign properties and methods to JavaScript objects, and create your own objects.
Java programs are kept in a special directories and files on the server machine. Programs are distinct and separate from HTML. JavaScript is fully integrated with HTML.
Java uses strong typing. Programmers must declare variable types in advance. JavaScript uses weak typing--in fact, script variables are virtually typeless. Variables are flexible and can contain any type of object, and the type can change during the execution of a script.
Java binds statically. All Object references must be defined at compile time. JavaScript binds dynamically. References are checked at run time.


How do I get started?

Just highlight the following and copy it into a fresh text file. Save the file as "FIRST.HTM" and load it into your browser. (This example does not attempt to "hide" JavaScript from old browsers.)
<H3>My First JavaScript HTML Document</H3>
<!-- Here's some Java Script-->

<SCRIPT LANGUAGE="JavaScript">
document.write("This is my first JavaScript document!")
</SCRIPT>



Where do I go next?

You can skip around between cookbook chapters in any order you want. However, if you are looking for a good introductory chapter, please start with the "Communicating with Readers" chapter. It introduces many important JavaScript concepts.

Copyright ©2000 by Charles River Media, All Rights Reserved